﻿using GuanYijia.Application.PurchaseManagement.PurchaseDTO;
using GuanYijia.Application.SupplierManagement.SupplierDTO;
using GuanYijia.DoMain;
using GuanYijia.DoMain.PageDto;
using GuanYijia.Repository;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Panda.DynamicWebApi;
using Panda.DynamicWebApi.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GuanYijia.Application.SupplierManagement
{
    [DynamicWebApi]
    [ApiExplorerSettings(GroupName = "v1")]
    public class SupplierDataService : IDynamicWebApi
    {
        private readonly IBaseRepository<Supplier> _supplierRepository;

        public SupplierDataService(IBaseRepository<Supplier> supplierRepository)
        {
            _supplierRepository = supplierRepository;
        }
        [HttpPost]
        public async Task<PageResultDto<SupplierRequisitionOutPut>> GetSupplierShow(SupplierRequisitionInPut supplierinput)
        {
            var list = new PageResultDto<SupplierRequisitionOutPut>() { TotalCount = 0, TotalPage = 0, Data = new List<SupplierRequisitionOutPut>() };
            var res = from a in  _supplierRepository.GetQueryable(tracking:false)
                      select new SupplierRequisitionOutPut
                      {
                          supplierId = a.SupplierId,
                          supplier = a.supplier,
                          contacts = a.contacts,
                          telephone = a.telephone,
                          phone_num = a.phone_num,
                          email = a.email,
                          begin_need_get = a.begin_need_get,
                          begin_need_pay = a.begin_need_pay,
                          tax_rate = a.tax_rate,
                          sort = a.sort,
                          enabled = a.enabled
                      };
            if (!string.IsNullOrEmpty(supplierinput.supplier))
            {
                res = res.Where(x => x.supplier.Contains(supplierinput.supplier));
            }
            if (!string.IsNullOrEmpty(supplierinput.telephone))
            {
                res = res.Where(x => x.telephone.Contains(supplierinput.telephone));
            }
            if (!string.IsNullOrEmpty(supplierinput.phone_num))
            {
                res = res.Where(x => x.phone_num.Contains(supplierinput.phone_num));
            }
            list.TotalCount = res.Count();
            list.TotalPage = (int)Math.Ceiling(list.TotalCount * 1.0 / supplierinput.pageSize);
            list.Data = await res.OrderBy(x => x.sort).Skip((supplierinput.pageIndex - 1) * supplierinput.pageSize).Take(supplierinput.pageSize).ToListAsync();
            return list;
        }
        /// <summary>
        /// 逻辑单删
        /// </summary>
        /// <param name="supplierId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<int> DeleteSupplier(int supplierId)
        {
            var requisition = await _supplierRepository.GetAsync(supplierId);
            requisition.delete_flag = true;
            return await _supplierRepository.UpdateAsync(requisition);
        }
        /// <summary>
        /// 逻辑批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> RangeDeleteSupplier(List<int> ids)
        {
            var requisitionList = await _supplierRepository.GetAllAsyncList(x => ids.Contains(x.SupplierId));
            foreach (var item in requisitionList)
            {
                item.delete_flag = true;
            }
            return await _supplierRepository.UpdateRangeAsync(requisitionList);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="supplier"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddSupplier(Supplier supplier)
        {
            var list = _supplierRepository.GetQueryable(tracking: false);
            if(list.Where(x=>x.supplier==supplier.supplier).Count() > 0)
            {
                return -1;
            }
            return await _supplierRepository.InsertAsync(supplier);
        }
        /// <summary>
        /// 启用
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> UpdateCanenable(List<int> ids)
        {
            var requisitionList = await _supplierRepository.GetAllAsyncList(x => ids.Contains(x.SupplierId));
            foreach (var item in requisitionList)
            {
                item.enabled = false;
            }
            return await _supplierRepository.UpdateRangeAsync(requisitionList);
        }
        /// <summary>
        /// 禁用
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> UpdateDisable(List<int> ids)
        {
            var requisitionList = await _supplierRepository.GetAllAsyncList(x => ids.Contains(x.SupplierId));
            foreach (var item in requisitionList)
            {
                item.enabled = true;
            }
            return await _supplierRepository.UpdateRangeAsync(requisitionList);
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="supplierId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Supplier> GetSuppLierInfo(int supplierId)
        {
            return await _supplierRepository.GetAsync(supplierId);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="supplier"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> UpdateSupplier(Supplier supplier)
        {
            var list = _supplierRepository.GetQueryable(tracking: false);
            if (list.Where(x => x.supplier == supplier.supplier).Count() > 0)
            {
                return -1;
            }
            return await _supplierRepository.UpdateAsync(supplier);
        }
    }
}
